<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Olddata;


class AjaxController extends Controller
{

    //关闭获取相关代理人处的csrf验证
    public function beforeAction($action) {

        $currentaction = $action->id;

        $novalidactions = ['ajaxagents'];

        if(in_array($currentaction,$novalidactions)) {

            $action->controller->enableCsrfValidation = false;
        }
        parent::beforeAction($action);

        return true;
    }



    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionAjaxagents()//与航空公司有关系的代理人
    {
        $curAirline=\Yii::$app->request->post('airline','C1');
        $curPage = \Yii::$app->request->post('page',0);
        if($curPage<0)
            $curPage=0;
        $pageSize=20;

        $agents= Olddata::find()->where(['sale_nbr'=>$curAirline])->limit($pageSize)->offset($curPage*$pageSize)->asArray()->all();
       // $agents= Olddata::find(['sale_nbr'=>$curAirline])->limit($pageSize)->offset($curPage*$pageSize)->asArray()->all();
     //   $sql='SELECT DISTINCT buy_nbr FROM olddata WHERE sale_nbr=\''.$curAirline.'\' ORDER BY sale_nbr LIMIT '.$curPage*$pageSize.','.$pageSize.';';
     //   $agents=Olddata::findBySql($sql)->asArray()->all();
        $r=[];
        $r['airline']=$curAirline;
        $r['page']=$curPage;
        $r['agents']=$agents;
        $r=json_encode($r);
        return $r;
    }

}
